package com.run.web.backend;


import com.run.model.EbNews;
import com.run.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;
    @RequestMapping("/list")
    public String list(Model model){
        List<EbNews> ebNews = newsService.list();
        model.addAttribute("ebNews",ebNews);
        return "manage/news";
    }

    @RequestMapping("/to_save")
    public String toSave(){
        return "manage/news-add";
    }
    @RequestMapping("/save")
    public String save(EbNews ebNews){

        Integer result = newsService.save(ebNews);
        if (result<1){
            //添加失败的情况
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }
    @RequestMapping("/to_edit")
    public String toEdit(Model model,@RequestParam("enId") int enId){
        System.out.println(enId);
        EbNews ebNews = newsService.selectOne(enId);
        model.addAttribute("enNews",ebNews);
        System.out.println(model.addAttribute("enNews",ebNews));
        return "manage/news-modify";
    }
    @RequestMapping("/edit")
    public String edit(EbNews ebNews){
        Integer reult = newsService.updateNews(ebNews);
        if (reult<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/delet")
    public String delet(Model model,@RequestParam("enId") int enId){
        int result = newsService.deltNews(enId);
        model.addAttribute("enNews",result);
        System.out.println(model.addAttribute("enNews",result));
        if (result<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }
}
